// 1. 当左边和上边都不是矩阵边界时，即 i != 0, j != 0
// 2. 当只有左边是矩阵边界时，即 i = 0, j != 0
// 3. 当只有上边是矩阵边界时，即 i != 0, j = 0

var minPathSum = function (grid) {
  for (let i = 0; i < grid.length; i++) {
    for (let j = 0; j < grid[0].length; j++) {
      if (i != 0 && j != 0) {
        grid[i][j] = Math.min(grid[i - 1][j], grid[i][j - 1]) + grid[i][j];
      } else if (i == 0 && j != 0) {
        grid[i][j] = grid[i][j - 1] + grid[i][j];
      } else if (i != 0 && j == 0) {
        grid[i][j] = grid[i - 1][j] + grid[i][j];
      } else if (i == 0 && j == 0) {
        continue;
      }
    }
  } 
  return grid[grid.length - 1][grid[0].length - 1];
}